package com.black.play.common.unit.standard;

import com.black.play.common.unit.UnitProvider;

/**
 * @author ylx
 */
public class BytesUnitProvider implements UnitProvider
{
    private static final int KB = 1024;

    private static final int MB = KB * 1024;

    private static final long GB = MB * 1024L;

    private static final long TB = GB * 1024L;

    private static final long PB = TB * 1024L;

    private static final double rate = 0.8;

    @Override
    public String get(Number value)
    {
        double speed = value.doubleValue();
        String unit;
        if (speed < KB * rate)
        {
            unit = "B";
        }
        else if (speed < MB * rate)
        {
            speed /= KB;
            unit = "KB";
        }
        else if (speed < GB * rate)
        {
            speed /= MB;
            unit = "MB";
        }
        else if (speed < TB * rate)
        {
            speed /= GB;
            unit = "GB";
        }
        else if (speed < PB * rate)
        {
            speed /= TB;
            unit = "TB";
        }
        else
        {
            speed /= PB;
            unit = "PB";
        }
        return String.format("%.2f%s", speed, unit);
    }
}
